package it.acweb.pauseless.pauselessapp;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BrowseMixActivity extends Activity {

	//private xml_utils ut;

	public void alert (String msg) {
		Toast.makeText(getApplicationContext(),
			      msg, Toast.LENGTH_LONG)
			      .show();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browse_cd);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);

		ListView listView = (ListView) findViewById(R.id.cdList);

		//inizializzo xml_utils
		xml_utils.setContext(getApplicationContext());

		try {

			Document doc = xml_utils.get_main_document(R.raw.mix_list);
            
            NodeList nodeList = doc.getElementsByTagName("mix");
            String[] values = new String[nodeList.getLength()];

            for (int i = 0; i < nodeList.getLength(); i++) {

            	Element el = (Element)nodeList.item(i);
                values[i] = el.getAttribute("name") + " - " + el.getAttribute("bpm");
            }

    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  		  	android.R.layout.simple_list_item_1, android.R.id.text1, values);

	  		// Assign adapter to ListView
	  		listView.setAdapter(adapter);

            
		} catch (Exception e) {
            System.out.println("BrowseMixActivity onCreate = " + e);
            e.printStackTrace();
        }		
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view,
			    int position, long id) {
					try {

			            /*Document doc = xml_utils.get_main_document(R.raw.cd_list);
			            
			            Node node = xml_utils.get_cd(doc, (String) parent.getItemAtPosition(position));
			            
			            Song[] songs = xml_utils.get_songs(node);
			            
			            Intent intent = new Intent(BrowseCdActivity.this, BrowseSongsActivity.class);
			            
			            Bundle bundle = new Bundle();
			            bundle.putParcelableArray("it.acweb.pauseless.pauselessapp.songs", songs);
			            intent.putExtras(bundle);
			            
			            //intent.putExtra("songs", songs);*/
			            Intent intent = new Intent(BrowseMixActivity.this, BrowseSongsMixActivity.class);
			            intent.putExtra("mix", (String) parent.getItemAtPosition(position));
			            startActivity(intent);

					} catch (Exception e) {
			            System.out.println("BrowseMixActivity listView.setOnItemClickListener = " + e);
			            e.printStackTrace();
			        }

				  				  
			  }
		}); 

	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_browse_cd, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
